package com.smartmining.r.project.business;

import com.smartmining.base.utils.SmartUtils;
import com.smartmining.r.bean.*;
import com.smartmining.r.project.exception.ReportException;
import com.smartmining.r.utils.ExcelUtil;
import com.smartmining.r.utils.WordUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
 *  云报表导出 word 文档
 * @author   caoshi
 * @created  2020/06/02 19:15
 */
@Component
public class ExportReportForWordBusiness {

    private static final Logger logger = LoggerFactory.getLogger(ExportReportForWordBusiness.class);

    private static ExportReportForWordBusiness instance = new ExportReportForWordBusiness();

    public ExportReportForWordBusiness() {
    }

    public static ExportReportForWordBusiness getInstance() {
        return instance;
    }

    /**
     * 导出word
     *
     * @param fileName 文件名
     * @param htmlStr  html串
     * @throws Exception
     */
    public void exportWord(String fileName, String htmlStr) throws Exception {
        XWPFDocument document = new XWPFDocument();
        try {
            List<ReportContent> contentList = parseHtml(htmlStr);
            writeToWord(contentList, document);
        } catch (Exception e) {
            throw e;
        }
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) return;
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        ExcelUtil.setHttpServletResponse(request, response, fileName + ".docx");
        document.write(response.getOutputStream());
    }

    public byte[] exportWordBytes(String htmlStr) throws Exception {
        XWPFDocument document = new XWPFDocument();
        try {
            List<ReportContent> contentList = parseHtml(htmlStr);
            writeToWord(contentList, document);

        } catch (Exception e) {
            throw e;
        }
        ByteArrayOutputStream templateout = new ByteArrayOutputStream();
        document.write(templateout);
        document.close();
        return templateout.toByteArray();
    }

    /**
     * 解析html
     *
     * @param html
     * @return
     */
    private List<ReportContent> parseHtml(String html) throws ReportException {
        // 解析得到的内容
        List<ReportContent> contentList = new ArrayList<>();
        Document document = Jsoup.parse(html);
        Element body = document.body();
        Elements elements = body.select("tr");

        for (int tr = 0; tr < elements.size(); tr++) {      // element td
            Element element = elements.get(tr);
            ReportContent rc = new ReportContent();
            Map<String, String> styleMap = new HashMap<>();
            parseStyle(element, styleMap);
            // td 里的元素
            Elements inTd = element.select("td>*");
            for (Element ele : inTd) {
                parseStyle(ele, styleMap);
            }

            // echarts 图表
            Elements chartCellBox = element.select("td>div.chartCellBox");
            if (chartCellBox != null && chartCellBox.size() > 0) {
                // 检测与图片同行是否有文本或表格
//                Elements tds = element.select("td");
////                if (tds != null && tds.size() > 1) {
////                    for (Element td : tds) {
////                        Elements select = td.select("div.title");
////                        if (select == null || select.size() == 0) {
////                            String text = td.text();
////                            if (SmartUtils.isNotNullAndBlank(text)) {
////                                throw new ReportException(-1, "图表与其他内容不应配置在同一行!");
////                            }
////                        }
////                    }
////                }
                Element chartImg = chartCellBox.select("img").get(0);
                String base64Str = chartImg.attr("src")
                        .replace("data:image/png;base64,", "")
                        .replace(" ", "");
                ByteArrayInputStream bis = WordUtil.generateImage(base64Str);

                Element imgParentTd = chartImg.parent().parent().nextElementSibling();
                if (imgParentTd != null) {
                    String text = imgParentTd.text();
                    if (SmartUtils.isNotNullAndBlank(text)) {
                        throw new ReportException(-1, "图表与其他内容(表格、文本)不应配置在同一行!");
                    }
                }
                // 获取图表标题
                String titleText = "";
                Map<String, String> titleStyle = null;
                Elements divs = chartCellBox.select("div.title");
                if (divs.size() > 0) {          // 标题由前端配置
                    titleText = divs.get(0).text();
                    titleStyle = WordUtil.parseCssStyle(divs.get(0));       // 标题字体样式
                }
                // 封装图表信息
                ChartInfo cInfo = new ChartInfo();
                cInfo.setBis(bis);
                cInfo.setTitleText(titleText);
                cInfo.setTitleStyle(titleStyle);
                // 获取样式(宽高)
                Map<String, String> imgStyle = WordUtil.parseCssStyle(chartImg);
                String widthStr = imgStyle.get("width").replace("px", "");
                int width = Integer.parseInt(widthStr);
                String heightStr = imgStyle.get("height").replace("px", "");
                int height = Integer.parseInt(heightStr);
                cInfo.setWidth(width);
                cInfo.setHeight(height);

                if (SmartUtils.isNotNullAndBlank(base64Str)) {
                    rc.setReportContentType(ReportContentType.ECHARTS);
                    rc.setContent(cInfo);
                    rc.setStyleMap(imgStyle);
//                    logger.debug("======{}======", base64Str);
                    //contentList.add(ReportContent.SEPARATOR);       // 每一个图表前加一个分隔标识
                    contentList.add(rc);
                    continue;
                }
            }

            // 文本, 包括普通文本和富文本
            if (!WordUtil.isTable(styleMap) && !WordUtil.isImage(styleMap)) {
                List<TextInfo> textInfoList = new ArrayList<>();
                List<String> boldText = new ArrayList<>();
                Elements tds = element.select("td");
                for (Element td : tds) {
                    if (WordUtil.eleHasChild(td) && td.child(0).hasClass("richTxt")) {       // 富文本
                        // 查找富文本
                        Elements richTxt = td.select("div.richTxt");
                        for (Element rt : richTxt) {        // rt 这里遍历出的是每个富文本块 div
                            Element parent = rt.parent();
                            Map<String, String> parentStyle = WordUtil.parseCssStyle(parent);
                            Elements children = rt.children();
                            Elements strongs = rt.select("strong");
                            for (Element strong : strongs) {
                                boldText.add(strong.text());
                            }
                            for (Element child : children) {        // child 这里遍历出的是每个有独立样式的文本片段
                                TextInfo textInfo = new TextInfo();
                                String text = child.text();
                                Map<String, String> childStyle = WordUtil.parseCssStyle(child);
                                parseStyle(child, childStyle);
                                // 是否整体居中
                                if (parentStyle.containsKey("text-align")) {
                                    childStyle.put("text-align", parentStyle.get("text-align"));
                                }
                                // 是否是斜体字
                                Elements ems = child.select("em");
                                if (SmartUtils.isNotNullAndBlank(ems)) {
                                    childStyle.put("font-style", "italic");
                                }
                                if (SmartUtils.isNotNullAndBlank(text)) {
                                    textInfo.setText(text);
                                    textInfo.setStyleMap(childStyle);
                                    textInfoList.add(textInfo);
                                }
                            }
                        }
                    } else {            // 普通文本
                        String text = td.text();
                        Map<String, String> style = WordUtil.parseCssStyle(td);
                        // parseStyle(td, style);
                        if (SmartUtils.isNotNullAndBlank(text) && WordUtil.isDisplay(style)) {
                            TextInfo textInfo = new TextInfo(text, style);
                            textInfoList.add(textInfo);
                        }
                    }
                }
                if (textInfoList.size() > 0) {
                    rc.setReportContentType(ReportContentType.TEXT);
                    rc.setContent(textInfoList);
                    rc.setStyleMap(styleMap);
                    rc.setBoldText(boldText);
                    contentList.add(ReportContent.SEPARATOR);
                }
            }

            // 普通图片
            else if (WordUtil.isImage(styleMap)) {
                String base64Str = styleMap.get("backgroundImg");
                ByteArrayInputStream bis = WordUtil.generateImage(base64Str);
                rc.setContent(bis);
                rc.setReportContentType(ReportContentType.PICTURE);
                rc.setStyleMap(styleMap);
            }
            // 表格
            // 加第二个判断的理由是: 当富文本上方配置了表格(含边框), 然后再将这个表格隐藏
            // 这样就会误判该隐藏的表格是个真实的表格.     && !WordUtil.isHideRow(element)
            else if (WordUtil.isTableRow(element)) {
                // 左侧空白列数
                int leftBlankCols = WordUtil.getLeftBlankCols(element);
                // 左侧隐藏列数
                int leftHideCols = WordUtil.getLeftHideCols(element);
                // 隐藏的行数
                int hideRows = WordUtil.getTableHideRows(element);
                Map<Integer, List<List<String>>> dataMap = new HashMap<>();     // 行号 - 行内容集合
                List<MergeInfo> mergeInfos = new ArrayList<>();     // 单元格合并参数
                Map<String, Map<String, String>> wCellStyle = new HashMap<>();   // 每个格子的样式
                int tableRows = WordUtil.getTableRows(element);     // 当前表格的行数
                String bColorStr = "000000";
                // 读取到第一行表格的后, 行索引就要跳过表格行数
                tr += tableRows - 1 + hideRows;
//                logger.debug("==={}===", tableRows);
                List<Element> elementList = new ArrayList<>();
                if (!WordUtil.isHideRow(element)) {
                    elementList.add(element);
                }
                Element next = element.nextElementSibling();
                while (WordUtil.isTableRow(next)) {
                    if (!WordUtil.isHideRow(next)) {
                        elementList.add(next);
                    }
                    next = next.nextElementSibling();
                }

//                for (int i = 0; i < tableRows; i++) {
//                    if (i == 0) {
//                        elementList.add(element);
//                    } else {
//                        if (!WordUtil.isHideRow(next)) {
//                            elementList.add(next);
//                        }
//                        next = next.nextElementSibling();
//                    }
//                }
                for (int i = 0; i < elementList.size(); i++) {      // 遍历行
                    Element rowEle = elementList.get(i);
                    Elements children = rowEle.children();
                    List<List<String>> rowDataList = new ArrayList<>();
                    int styleCol = 0;
                    for (int j = 0; j < children.size(); j++) {     // 遍历列
                        Element child = children.get(j);        // 每一个 td单元格
//                        logger.debug("{} === {}", i, j);
                        Map<String, String> cellStyle = WordUtil.parseCssStyle(child);
                        parseStyle(child, styleMap);
                        if (j == 0 && cellStyle.containsKey("border-color")) {
                            String bColorRGB = cellStyle.get("border-color");
                            bColorStr = WordUtil.rgb2ColorStr(bColorRGB);
                        }
                        // 单元格内容
                        List<String> textList = new ArrayList<>();
                        Map<String, String> childStyle = WordUtil.parseCssStyle(child);
                        String text = child.text();
                        if (StringUtils.isEmpty(text)) {
                            Elements divs = child.getElementsByTag("div");
                            if (divs != null && divs.size() > 0) {
                                Element el1 = divs.get(0);
//                                text = el1.attr("title");   // 文本控件悬停提示
                                Elements controls = el1.children();
                                if (controls != null && !controls.isEmpty()) {
                                    for (Element control : controls) {
                                        String name = control.tag().getName();
                                        if ("input".equals(name) || "textarea".equals(name)) {
                                            text = control.attr("evalue");
                                        }
                                    }
                                }
                            }
                        }
                        // 当前列号
                        int colIndex = WordUtil.getColIndex(child);
//                        if (!WordUtil.isDisplay(childStyle)) {
//                            colIndex -= 1;
//                        }
                        if (WordUtil.isDisplay(childStyle) && WordUtil.isTable(childStyle)) {
                            // 单元格样式
//                            wCellStyle.put(i + "-" + (colIndex - leftBlankCols - leftHideCols), cellStyle);
                            wCellStyle.put(i + "-" + styleCol, cellStyle);
                            styleCol++;
                            textList.add(text);
                        }
                        if (textList.size() > 0) {          // 装载单元格数据
                            rowDataList.add(textList);
                            // 行数据
                            dataMap.put(i, rowDataList);
                        }
                        // 记录单元格合并情况
                        int colspan = WordUtil.getColspanByElement(child);      // 横向
                        int rowspan = WordUtil.getRowspanByElement(child);      // 纵向
                        if (rowspan > 1 && colspan <= 1) {              // 只存在纵向合并
                            MergeInfo mergeInfo = new MergeInfo();
                            mergeInfo.setMergeDir("v");
                            mergeInfo.setCol(WordUtil.getColIndex(child) - leftBlankCols - leftHideCols);  // 哪一列出现纵向合并
                            mergeInfo.setFrom(i);
                            mergeInfo.setTo(i + rowspan - 1);
                            mergeInfos.add(mergeInfo);
                            wCellStyle.put(i + "-" + (j - 1), cellStyle);
                        }
                        if (colspan > 1 && rowspan <= 1) {              // 只存在横向合并
                            MergeInfo mergeInfo = new MergeInfo();
                            mergeInfo.setMergeDir("h");
                            mergeInfo.setRow(i);
                            mergeInfo.setFrom(WordUtil.getColIndex(child) - leftBlankCols - leftHideCols);
                            mergeInfo.setTo(WordUtil.getColIndex(child) - leftBlankCols -leftHideCols + colspan - 1);
                            mergeInfos.add(mergeInfo);
                        }
                        if (colspan > 1 && rowspan > 1) {       // 两种合并情况同时存在
//                            logger.debug("{} ==== {}", colspan, rowspan);
                            // 添加纵向合并
                            for (int k = 0; k < colspan; k++) {
                                MergeInfo mergeInfo = new MergeInfo();
                                mergeInfo.setMergeDir("v");
                                mergeInfo.setCol(WordUtil.getColIndex(child) - leftBlankCols + k);
                                mergeInfo.setFrom(i);
                                mergeInfo.setTo(i + rowspan - 1);
                                mergeInfos.add(mergeInfo);
                                wCellStyle.put(i + "-" + (j - 1), cellStyle);
                            }
                            // 添加横向合并
                            for (int k = 0; k < rowspan; k++) {
                                MergeInfo mergeInfo = new MergeInfo();
                                mergeInfo.setMergeDir("h");
                                mergeInfo.setRow(i);
                                mergeInfo.setFrom(WordUtil.getColIndex(child) - leftBlankCols - leftHideCols);
                                mergeInfo.setTo(WordUtil.getColIndex(child) - leftBlankCols -leftHideCols + colspan - 1);
                                mergeInfos.add(mergeInfo);
                            }
                        }

                    }
                }
                TableInfo tInfo = new TableInfo();
                tInfo.setRows(tableRows);                  // 表格行数
                // 计算表格列数, 取一行中列数最多的值
                int cols = WordUtil.getMaxCols(dataMap);
                tInfo.setCols(cols);      // 表格列数
                tInfo.setWCellStyle(wCellStyle);
                tInfo.setDataMap(dataMap);
                tInfo.setMergeInfos(mergeInfos);
                tInfo.setLeftBlankCols(leftBlankCols);      // 左侧空列数
                tInfo.setLeftHideCols(leftHideCols);        // 左侧隐藏列数
                tInfo.setBorderColorStr(bColorStr);
                rc.setReportContentType(ReportContentType.TABLE);
                rc.setContent(tInfo);
                rc.setStyleMap(styleMap);
            } else if (WordUtil.isInputControl(element)) {
                List<TextInfo> textInfoList = new ArrayList<>();
                TextInfo textInfo = new TextInfo();
                List<String> textStrList = new ArrayList<>();
                Elements children = element.children();
                for (Element child : children) {
                    String text = child.text();
                    if (StringUtils.isNotBlank(text)) {
                        textStrList.add(text);
                    } else {
                        String title = child.select("div").attr("title");
                        textStrList.add(title);
                    }
                }
                String rowtext = StringUtils.join(textStrList, " ");
                textInfo.setText(rowtext);
                textInfoList.add(textInfo);
                rc.setReportContentType(ReportContentType.TEXT);
                rc.setContent(textInfoList);
            }

            // 一行内容为空的不加空字符串, 而是加一个分隔标识
            if (SmartUtils.isNotNullAndBlank(rc.getContent())) {
                contentList.add(rc);
            } else {
                int size = contentList.size();
                if (size > 1) {
                    ReportContent c = contentList.get(size - 1);
                    if (c.getReportContentType() != ReportContentType.SEPARATOR) {
                        ReportContent cont = new ReportContent();
                        // cont.setType(ReportContent.TYPE_SEPARATOR);
                        cont.setReportContentType(ReportContentType.SEPARATOR);
                        contentList.add(cont);
                    }
                }
            }
        }
        return contentList;
    }

    /**
     * 解析样式
     *
     * @param ele
     * @param styleMap
     */
    private void parseStyle(Element ele, Map<String, String> styleMap) {
        // 父标签上的样式
        Element parent = ele.parent();
        Map<String, String> parentStyle = WordUtil.parseCssStyle(parent);
        styleMap.putAll(parentStyle);

        // 子代标签上的样式
        Elements children = ele.children();
        if (children.size() == 0)
            return;

        for (Element child : children) {
            Map<String, String> childStyle = WordUtil.parseCssStyle(child);
            styleMap.putAll(childStyle);
            if (child.children().size() > 0) {
                parseStyle(child, styleMap);
            }
        }
    }

    /**
     * 报表内容写入Word
     *
     * @param contents
     * @param document
     * @throws IOException
     * @throws InvalidFormatException
     */
    private void writeToWord(List<ReportContent> contents, XWPFDocument document) throws IOException, InvalidFormatException {
        if (contents != null && contents.size() > 0) {
            for (ReportContent content : contents) {
                if (SmartUtils.isNotNullAndBlank(content.getReportContentType())) {
                    switch (content.getReportContentType()) {
                        case TEXT:
                            writeText(content, document);
                            break;
                        case TABLE:
                            writeTable(content, document);
                            break;
                        case PICTURE:
                            writePicture(content, document);
                            break;
                        case ECHARTS:
                            writeEcharts(content, document);
                            break;
                        case SEPARATOR:     // 换行
                            document.createParagraph();
                    }
                }
            }
        }
    }

    /**
     * 写入文本
     *
     * @param content
     * @param document
     */
    private static void writeText(ReportContent content, XWPFDocument document) {
        List<TextInfo> textInfoList = (List<TextInfo>) content.getContent();
        XWPFParagraph txtP = document.createParagraph();
        // 设置段落样式
        WordUtil.setParagraphStyle(content.getStyleMap(), txtP);
        List<String> boldText = content.getBoldText();
        XWPFRun txtRun;
        for (TextInfo textInfo : textInfoList) {
            txtRun = txtP.createRun();
            WordUtil.setFontStyle(textInfo.getStyleMap(), txtRun);
            String text = textInfo.getText();
            if (SmartUtils.isNotNullAndBlank(text)) {
                if (boldText.contains(text)) {
                    txtRun.setBold(true);
                }
                txtRun.setText(text);
                txtRun.addBreak();
            }
        }
    }

    /**
     * 写入表格
     *
     * @param content
     * @param document
     */
    private static void writeTable(ReportContent content, XWPFDocument document) {
        TableInfo tInfo = (TableInfo) content.getContent();
        Map<String, Map<String, String>> wCellStyle = tInfo.getWCellStyle();
        // 行数, 列数
        Integer rows = tInfo.getRows();
        Integer cols = tInfo.getCols();
        // 防止创建无效空格i
        if (rows == 0 || cols == 0) {
            return;
        }
        // 数据
        Map<Integer, List<List<String>>> dataMap = tInfo.getDataMap();
        // 单元格合并情况
        List<MergeInfo> mergeInfos = tInfo.getMergeInfos();
        // 处理合并情况下单元格内容填充
        Set<String> mergeAreaCoords = MergeInfo.getMergeAreaCoords(mergeInfos);
        // 在有合并的单元格对应的数据设置为空字符串
        for (int i = 0; i < rows; i++) {
            List<List<String>> lists = dataMap.get(i);
            if (SmartUtils.isNotNullAndBlank(lists)) {
                for (int j = 0; j <= lists.size(); j++) {
                    if (mergeAreaCoords.contains(i + "-" + j)) {
                        List<String> temp = new ArrayList<>();
                        temp.add("");
                        lists.add(j, temp);
                    }
                }
                if (lists.size() > cols) {
                    cols = lists.size();     // 计算表格最大列数
                }
            }
        }

        // 创建表格
        XWPFTable table = document.createTable(rows, cols);
        // 左侧空白列数
        int leftBlankCols = tInfo.getLeftBlankCols();
        int leftHideCols = tInfo.getLeftHideCols();
        // 左侧空白列数(包括隐藏的列数) >=3表格整体居中
        if (leftBlankCols + leftHideCols >= 3 && table != null) {
            table.setTableAlignment(TableRowAlign.CENTER);
        }

        // 处理单元格合并
        for (MergeInfo mergeInfo : mergeInfos) {
            if ("v".equals(mergeInfo.getMergeDir())) {
                WordUtil.mergeCellsVertically(table, mergeInfo.getCol(), mergeInfo.getFrom(), mergeInfo.getTo());               // 纵向合并, 合并行
            }
            else if ("h".equals(mergeInfo.getMergeDir())) {
                WordUtil.mergeCellsHorizontal(table, mergeInfo.getRow(), mergeInfo.getFrom(), mergeInfo.getTo());               // 横向合并, 合并列
            }
        }

        // 保证每个单元格都设置宽度
        for (int i = 0; i < rows; i++) {
            XWPFTableRow row = table.getRow(i);
            for (int j = 0; j < cols; j++) {
                XWPFTableCell cell = row.getTableCells().get(j);
                cell.setWidth("1200");
            }
        }
        WordUtil.adaptDocumentWidth(document, cols);
        // 填充数据
        for (int i = 0; i < rows; i++) {
            XWPFTableRow row = table.getRow(i);
            List<List<String>> lists = dataMap.get(i);
            if (lists != null) {
                for (int j = 0; j < lists.size(); j++) {
                    XWPFTableCell cell = row.getTableCells().get(j);
                    cell.setWidth("1200");
                    String s = lists.get(j).get(0);
                    // 设置单元格样式
                    WordUtil.setTableCellStyle(cell, i, j, wCellStyle);
                    cell.setText(s);
                }
            }
        }
        // 设置边框颜色
        WordUtil.setTabBorderColor(table, tInfo.getBorderColorStr());
        //document.createParagraph();
    }

    /**
     * 插入图片(普通图片, 不包括Echarts图表)
     *
     * @param content
     * @param document
     * @throws IOException
     * @throws InvalidFormatException
     */
    private static void writePicture(ReportContent content, XWPFDocument document) throws IOException, InvalidFormatException {
        Map<String, String> styleMap = content.getStyleMap();
        InputStream is = (InputStream) content.getContent();
        // 从样式Map中读取图片宽高
        String widthStr = styleMap.get("imgWidth").replace("px", "");
        int width = Integer.parseInt(widthStr);
        String heightStr = styleMap.get("imgHeight").replace("px", "");
        int height = Integer.parseInt(heightStr);
        // logger.debug("图片宽高 >> {} -- {}", width, height);
        XWPFParagraph pa = document.createParagraph();
        XWPFRun run = pa.createRun();
        WordUtil.setParagraphStyle(styleMap, pa);
        // 这块的逻辑是: 如果图片的宽度大于600, 600是个像素值, 在word里刚好居中显示.
        if (width > 600) {
            width = 600;
        }
        run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, "test", Units.pixelToEMU(width), Units.pixelToEMU(height));
        run.addBreak();
    }

    private static void writeEcharts(ReportContent content, XWPFDocument document) throws IOException, InvalidFormatException {
        ChartInfo info = (ChartInfo) content.getContent();
        String title = info.getTitleText();
        Map<String, String> titleStyle = info.getTitleStyle();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        if (SmartUtils.isNotNullAndBlank(title)) {
            paragraph.setAlignment(ParagraphAlignment.CENTER);      // Echarts图表标题设置居中
            WordUtil.setFontStyle(titleStyle, run);
            run.setText(title);
            run.addBreak();
        }
        if (info.getWidth() > 600) {
            info.setWidth(600);
        }
        run.addPicture(info.getBis(), XWPFDocument.PICTURE_TYPE_PNG, "test",
                Units.pixelToEMU(info.getWidth()), Units.pixelToEMU(info.getHeight()));
    }


}
